﻿namespace IP_Package
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// THE MAIN FORM
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// object of type open
        /// </summary>
        private IPPackage.ImageOperation.Open img = new IPPackage.ImageOperation.Open();

        /// <summary>
        /// object of type open to open second img
        /// </summary>
        private IPPackage.ImageOperation.Open img2 = new IPPackage.ImageOperation.Open();

        /// <summary>
        /// the processing image
        /// </summary>
        private Bitmap image;

        /// <summary>
        /// Initializes a new instance of the <see cref="Form1"/> class.
        /// </summary>
        public Form1()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the openToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string opendFileName = openFileDialog1.FileName;
                this.image = this.img.OpenImage(opendFileName);
                this.PicImageOrginal.Image = this.image;
                ////set Image information
                this.lbHieght.Text = this.image.Height.ToString();
                this.lbWidth.Text = this.image.Width.ToString();
                string[] fileFormat = opendFileName.Split('.');
                this.lbImageFormat.Text = fileFormat[1] + " file";               
                ////this.picbBrit_contrast.Image = this.image;
            }
        }

        /// <summary>
        /// Handles the Click event of the exitToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the translateToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TranslateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GUI.Translate new_Tran = new IP_Package.GUI.Translate();
            new_Tran.SetInfo(this.PicImageOrginal.Image, this.lbWidth.Text, this.lbHieght.Text, this.img.BufferOfImage);
            new_Tran.ShowDialog();
            if (new_Tran.SaveOrNot1 == true)
            {
                this.img.BufferOfImage = new_Tran.BufferOfImage;
                this.image = new_Tran.ResultImg;
                this.PicImageOrginal.Image = this.image;
            }
        }

        /// <summary>
        /// Handles the Click event of the reSizeToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ReSizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GUI.ResizeF new_Resize = new IP_Package.GUI.ResizeF();
            new_Resize.SetInfo(this.PicImageOrginal.Image, this.lbWidth.Text, this.lbHieght.Text, this.img.BufferOfImage);
            new_Resize.ShowDialog();
            if (new_Resize.SaveOrNot1 == true)
            {
                this.img.BufferOfImage = new_Resize.BufferOfImage;
                this.image = new_Resize.ResultImg;
                this.PicImageOrginal.Image = this.image;
                this.lbWidth.Text = this.img.BufferOfImage.GetLength(0).ToString();
                this.lbHieght.Text = this.img.BufferOfImage.GetLength(1).ToString();
            }
        }

        /// <summary>
        /// Handles the Click event of the grayToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GrayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap newImg = this.img.GrayImg();
            if (newImg == null)
            {
                MessageBox.Show("must insert image to make it gray");
            }
            else
            {
                this.PicImageOrginal.Image = newImg;
                this.img.BufferOfImage = this.img.NewBufferOfImage;
            }     
        }

        /// <summary>
        /// Handles the Click event of the histogramToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void HistogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IPPackage.ImageOperation.Histogram graph = new IPPackage.ImageOperation.Histogram();
            graph.BufferOfImage = this.img.BufferOfImage;
            if (graph.BufferOfImage == null)
            {
                MessageBox.Show("no image to draw histogram");
            }

            if (this.checkGray.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGray();
                Bitmap graphImg = graph.Draw(graph1, Color.Gray);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkRed.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoRed();
                Bitmap graphImg = graph.Draw(graph1, Color.Red);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkBlue.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoBlue();
                Bitmap graphImg = graph.Draw(graph1, Color.Blue);
                this.pbHistograph.Image = graphImg;
            }

            if (this.CheckGreen.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGreen();
                Bitmap graphImg = graph.Draw(graph1, Color.Green);
                this.pbHistograph.Image = graphImg;
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckGreen control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckGreen_CheckedChanged(object sender, EventArgs e)
        {
            IPPackage.ImageOperation.Histogram graph = new IPPackage.ImageOperation.Histogram();
            graph.BufferOfImage = this.img.BufferOfImage;
            if (graph.BufferOfImage == null)
            {
                MessageBox.Show("no image to draw histogram");
            }

            if (this.checkGray.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGray();
                Bitmap graphImg = graph.Draw(graph1, Color.Gray);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkRed.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoRed();
                Bitmap graphImg = graph.Draw(graph1, Color.Red);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkBlue.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoBlue();
                Bitmap graphImg = graph.Draw(graph1, Color.Blue);
                this.pbHistograph.Image = graphImg;
            }

            if (this.CheckGreen.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGreen();
                Bitmap graphImg = graph.Draw(graph1, Color.Green);
                this.pbHistograph.Image = graphImg;
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckGray control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckGray_CheckedChanged(object sender, EventArgs e)
        {
            IPPackage.ImageOperation.Histogram graph = new IPPackage.ImageOperation.Histogram();
            graph.BufferOfImage = this.img.BufferOfImage;
            if (graph.BufferOfImage == null)
            {
                MessageBox.Show("no image to draw histogram");
            }

            if (this.checkGray.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGray();
                Bitmap graphImg = graph.Draw(graph1, Color.Gray);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkRed.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoRed();
                Bitmap graphImg = graph.Draw(graph1, Color.Red);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkBlue.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoBlue();
                Bitmap graphImg = graph.Draw(graph1, Color.Blue);
                this.pbHistograph.Image = graphImg;
            }

            if (this.CheckGreen.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGreen();
                Bitmap graphImg = graph.Draw(graph1, Color.Green);
                this.pbHistograph.Image = graphImg;
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckRed control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckRed_CheckedChanged(object sender, EventArgs e)
        {
            IPPackage.ImageOperation.Histogram graph = new IPPackage.ImageOperation.Histogram();
            graph.BufferOfImage = this.img.BufferOfImage;
            if (graph.BufferOfImage == null)
            {
                MessageBox.Show("no image to draw histogram");
            }

            if (this.checkGray.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGray();
                Bitmap graphImg = graph.Draw(graph1, Color.Gray);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkRed.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoRed();
                Bitmap graphImg = graph.Draw(graph1, Color.Red);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkBlue.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoBlue();
                Bitmap graphImg = graph.Draw(graph1, Color.Blue);
                this.pbHistograph.Image = graphImg;
            }

            if (this.CheckGreen.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGreen();
                Bitmap graphImg = graph.Draw(graph1, Color.Green);
                this.pbHistograph.Image = graphImg;
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBlue control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckBlue_CheckedChanged(object sender, EventArgs e)
        {
            IPPackage.ImageOperation.Histogram graph = new IPPackage.ImageOperation.Histogram();
            graph.BufferOfImage = this.img.BufferOfImage;
            if (graph.BufferOfImage == null)
            {
                MessageBox.Show("no image to draw histogram");
            }

            if (this.checkGray.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGray();
                Bitmap graphImg = graph.Draw(graph1, Color.Gray);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkRed.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoRed();
                Bitmap graphImg = graph.Draw(graph1, Color.Red);
                this.pbHistograph.Image = graphImg;
            }

            if (this.checkBlue.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoBlue();
                Bitmap graphImg = graph.Draw(graph1, Color.Blue);
                this.pbHistograph.Image = graphImg;
            }

            if (this.CheckGreen.Checked == true)
            {
                long[] graph1;
                graph1 = graph.HistoGreen();
                Bitmap graphImg = graph.Draw(graph1, Color.Green);
                this.pbHistograph.Image = graphImg;
            }
        }

        /// <summary>
        /// Handles the Click event of the brightnessToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BrightnessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GUI.ContrastAndBrightness new_brightness = new IP_Package.GUI.ContrastAndBrightness();
            new_brightness.SetInfo(this.PicImageOrginal.Image, this.lbWidth.Text, this.lbHieght.Text, this.img.BufferOfImage);
            new_brightness.ShowDialog();
            if (new_brightness.SaveOrNot1 == true)
            {
                this.img.BufferOfImage = new_brightness.BufferOfImage;
                this.image = new_brightness.ResultImg;
                this.PicImageOrginal.Image = this.image;
            }
        }

        /// <summary>
        /// Handles the Click event of the ContrToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ContrToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GUI.ContrastAndBrightness new_contrast = new IP_Package.GUI.ContrastAndBrightness();
            new_contrast.SetInfo(this.PicImageOrginal.Image, this.lbWidth.Text, this.lbHieght.Text, this.img.BufferOfImage);
            new_contrast.ShowDialog();
            if (new_contrast.SaveOrNot1 == true)
            {
                this.img.BufferOfImage = new_contrast.BufferOfImage;
                this.image = new_contrast.ResultImg;
                this.PicImageOrginal.Image = this.image;
            }
        }

        /// <summary>
        /// Handles the Click event of the GToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GUI.GamaForm new_gamma = new IP_Package.GUI.GamaForm();
            new_gamma.SetInfo(this.PicImageOrginal.Image, this.lbWidth.Text, this.lbHieght.Text, this.img.BufferOfImage);
            new_gamma.ShowDialog();
            if (new_gamma.SaveOrNot1 == true)
            {
                this.img.BufferOfImage = new_gamma.BufferOfImage;
                this.image = new_gamma.ResultImg;
                this.PicImageOrginal.Image = this.image;
           }
        }

        /// <summary>
        /// Handles the Click event of the equlizationToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void EqulizationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IPPackage.ImageOperation.HistoOperation hisEualization = new IPPackage.ImageOperation.HistoOperation();
            hisEualization.BufferOfImage = this.img.BufferOfImage;
            try
            {
                this.image = hisEualization.Equalization();
                this.PicImageOrginal.Image = this.image;
                this.img.BufferOfImage = hisEualization.NewBufferOfImage;
            }
            catch (Exception ex)
            {
                IPPackage.Logger.LogException(ex);
                MessageBox.Show("operation fail make sure your data");
            }
        }

        /// <summary>
        /// Handles the Click event of the NegativeToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void NegativeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IPPackage.ImageAlgebra.OneImgOperation negativeOper = new IPPackage.ImageAlgebra.OneImgOperation();
            negativeOper.BufferOfImage = this.img.BufferOfImage;
            Bitmap newImg = negativeOper.MakeNegative();
            this.PicImageOrginal.Image = newImg;
            this.img.BufferOfImage = negativeOper.NewBufferOfImage;
            if (newImg == null)
            {
                MessageBox.Show("please insert image to make this operation");
            }
        }

        /// <summary>
        /// Handles the Click event of the AddOrSubToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void AddOrSubToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GUI.Calculation_form new_ope = new IP_Package.GUI.Calculation_form();
            new_ope.SetInfo(this.PicImageOrginal.Image, this.lbWidth.Text, this.lbHieght.Text, this.img.BufferOfImage);
            new_ope.ShowDialog();
            if (new_ope.SaveOrNot1 == true)
            {
                this.img.BufferOfImage = new_ope.BufferOfImage;
                this.image = new_ope.ResultImg;
                this.PicImageOrginal.Image = this.image;
            }
        }

        /// <summary>
        /// Handles the Click event of the quantizationToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void QuantizationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GUI.QuantizationForm new_qua = new IP_Package.GUI.QuantizationForm();
            new_qua.SetInfo(this.PicImageOrginal.Image, this.lbWidth.Text, this.lbHieght.Text, this.img.BufferOfImage);
            new_qua.ShowDialog();
            if (new_qua.SaveOrNot1 == true)
            {
                this.img.BufferOfImage = new_qua.BufferOfImage;
                this.image = new_qua.ResultImg;
                this.PicImageOrginal.Image = this.image;
            }
        }
       }
}
